#!/bin/bash

# WFH CLI 配置管理工具

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

CONFIG_FILE="$HOME/.gemini/.env"

print_header() {
    echo -e "${CYAN}"
    cat << 'EOF'
╔══════════════════════════════════════════════════════╗
║         🔧 WFH CLI - 配置管理工具 🔧               ║
╚══════════════════════════════════════════════════════╝
EOF
    echo -e "${NC}"
}

print_info() { echo -e "${BLUE}ℹ${NC} $1"; }
print_success() { echo -e "${GREEN}✓${NC} $1"; }
print_warning() { echo -e "${YELLOW}⚠${NC} $1"; }
print_error() { echo -e "${RED}✗${NC} $1"; }

# 显示当前配置
show_config() {
    print_header
    
    if [ ! -f "$CONFIG_FILE" ]; then
        print_warning "配置文件不存在: $CONFIG_FILE"
        return
    fi
    
    source "$CONFIG_FILE"
    
    echo ""
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${YELLOW}当前配置：${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo ""
    
    # 模型配置
    if [ -n "$LLM_PROVIDER" ]; then
        echo -e "${GREEN}模型提供商:${NC} $LLM_PROVIDER"
    else
        echo -e "${RED}模型提供商:${NC} 未设置"
    fi
    
    if [ -n "$LLM_MODEL" ]; then
        echo -e "${GREEN}模型版本:${NC} $LLM_MODEL"
    else
        echo -e "${YELLOW}模型版本:${NC} 使用默认"
    fi
    
    # API Key 检查
    case "$LLM_PROVIDER" in
        deepseek)
            if [ -n "$DEEPSEEK_API_KEY" ]; then
                echo -e "${GREEN}DeepSeek Key:${NC} ${DEEPSEEK_API_KEY:0:10}..."
            else
                echo -e "${RED}DeepSeek Key:${NC} 未设置"
            fi
            ;;
        qwen)
            if [ -n "$QWEN_API_KEY" ]; then
                echo -e "${GREEN}Qwen Key:${NC} ${QWEN_API_KEY:0:10}..."
            else
                echo -e "${RED}Qwen Key:${NC} 未设置"
            fi
            ;;
        kimi)
            if [ -n "$KIMI_API_KEY" ]; then
                echo -e "${GREEN}Kimi Key:${NC} ${KIMI_API_KEY:0:10}..."
            else
                echo -e "${RED}Kimi Key:${NC} 未设置"
            fi
            ;;
        doubao)
            if [ -n "$DOUBAO_API_KEY" ]; then
                echo -e "${GREEN}Doubao Key:${NC} ${DOUBAO_API_KEY:0:10}..."
            else
                echo -e "${RED}Doubao Key:${NC} 未设置"
            fi
            ;;
        glm)
            if [ -n "$GLM_API_KEY" ]; then
                echo -e "${GREEN}GLM Key:${NC} ${GLM_API_KEY:0:10}..."
            else
                echo -e "${RED}GLM Key:${NC} 未设置"
            fi
            ;;
    esac
    
    echo ""
    
    # 搜索配置
    if [ -n "$SEARCH_ENGINE" ]; then
        echo -e "${GREEN}搜索引擎:${NC} $SEARCH_ENGINE"
    else
        echo -e "${YELLOW}搜索引擎:${NC} duckduckgo (默认)"
    fi
    
    if [ "$SEARCH_ENGINE" = "tavily" ] && [ -n "$TAVILY_API_KEY" ]; then
        echo -e "${GREEN}Tavily Key:${NC} ${TAVILY_API_KEY:0:10}..."
    fi
    
    echo ""
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}

# 快速设置默认模型
quick_set() {
    local provider=$1
    
    if [ -z "$provider" ]; then
        echo "用法: wfh-config set <provider>"
        echo "可选值: deepseek, qwen, kimi, doubao, glm, openai"
        return 1
    fi
    
    case "$provider" in
        deepseek)
            ./wfh-select-model.sh
            # 自动选择 1 和 1
            ;;
        qwen|kimi|doubao|glm|openai)
            ./wfh-select-model.sh
            ;;
        *)
            print_error "未知的提供商: $provider"
            echo "可选值: deepseek, qwen, kimi, doubao, glm, openai"
            return 1
            ;;
    esac
}

# 主函数
main() {
    case "${1:-show}" in
        show|config|list)
            show_config
            ;;
        set|select|choose)
            if [ -n "$2" ]; then
                quick_set "$2"
            else
                ./wfh-select-model.sh
            fi
            ;;
        edit)
            ${EDITOR:-nano} "$CONFIG_FILE"
            ;;
        reset)
            if [ -f "$CONFIG_FILE" ]; then
                mv "$CONFIG_FILE" "$CONFIG_FILE.backup"
                print_success "配置已重置，备份保存在: $CONFIG_FILE.backup"
            fi
            ;;
        help|-h|--help)
            print_header
            cat << 'EOF'
用法:
  wfh-config [command]

命令:
  show              显示当前配置 (默认)
  set [provider]    设置模型提供商
  edit              编辑配置文件
  reset             重置配置

示例:
  wfh-config                  # 显示配置
  wfh-config set              # 交互式选择模型
  wfh-config set deepseek     # 快速设置 DeepSeek
  wfh-config edit             # 编辑配置文件

EOF
            ;;
        *)
            print_error "未知命令: $1"
            echo "运行 'wfh-config help' 查看帮助"
            ;;
    esac
}

main "$@"

